define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'works_cap/index' + location.search,
                    add_url: 'works_cap/add',
                    edit_url: 'works_cap/edit',
                    del_url: 'works_cap/del',
                    multi_url: 'works_cap/multi',
                    import_url: 'works_cap/import',
                    table: 'works_cap',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [

                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'nickname', title: __('User_id') },
                        { field: 'typsNick', title: __('扣款类型') },
                        { field: 'money', title: __('扣款金额'), operate: 'BETWEEN' },
                        { field: 'repayMoney', title: __('已还金额'), operate: 'BETWEEN' },
                        { field: 'outAmt', title: __('未还金额'), operate: 'BETWEEN' },
                        { field: 'balance', title: __('当前余额'), operate: 'BETWEEN' },
                        { field: 'typs', title: __('Typs'), searchList: { "Z": __('Typs z'), "J": __('Typs j') }, formatter: Table.api.formatter.normal },
                        { field: 'status', title: __('Status'), searchList: { "1": __('Status 1'), "2": __('Status 2') }, formatter: Table.api.formatter.status },
                        { field: 'is_repay', title: __('Is_repay'), searchList: { "1": __('Is_repay 1'), "2": __('Is_repay 2'), "3": __('Is_repay 3') }, formatter: Table.api.formatter.normal },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            Controller.api.bindevent();
            $('#c-user_id').change(function() {
                var userId = $("#c-user_id").val()
                console.log(userId)
                $('#c-repayMoney').val("");
                if (userId) {
                    $.ajax({
                        url: 'works_cap/balance', // 假设的API接口
                        type: 'get',
                        data: { userId: userId },
                        dataType: 'json',
                        success: function(ret) {
                            $('#c-money-current').val(ret.money)
                        }
                    });
                }
            });
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }

        }
    };
    return Controller;
});